package com.yc.fav.demo;

import java.lang.annotation.*;

@MyAnno(name="root", value = "demo", chars = {'c'})
public class AnnoDemo {

    @Anno1(100) // value = 100
    int b;
    @Anno1(value = 100, price = 2.0f)
    int a;
    @Anno1(value = 100, price = {2.0f,3.0f})
    int c;

    @MyAnno(name="root", value = "demo", chars = {'c'})
    public void test(){

    }

    public static void main(String[] args) {
        // 获取类上的注解
        MyAnno anno = AnnoDemo.class.getAnnotation(MyAnno.class);
        // 输出注解的属性值
        System.out.println(anno.name());
        System.out.println(anno.value());

        System.out.println("========== 注解的继承特性 ===========");
        // 获取方法上的注解
        MyAnno anno1 = AnnoDemo.class.getAnnotation(MyAnno.class);
        System.out.println(anno1.name());
        System.out.println(anno1.value());

        System.out.println("---------------- 子类的test方法获取注解-----------------");
        // 获取方法上的注解
        MyAnno anno2 = SubAnnoDemo.class.getAnnotation(MyAnno.class);
        System.out.println(anno2.name());
        System.out.println(anno2.value());
    }

}

class SubAnnoDemo extends AnnoDemo{

}


// 运行时有效
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Inherited // 继承注解 => 类上注解的继承
@interface MyAnno{
    String name(); // 注解属性name, 方法名 => 属性名
    String value(); // value
    int age() default 10;
    char[] chars();
}

@Target(ElementType.FIELD)
@interface Anno1{
    int value();
    float[] price() default 1.0f;
}
